﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TurnamentOrganizer
{
    public partial class ResultsForm : Form
    {
        Tournament _main;
        public BindingList<Player> SelectedPlayerList { get; set; }

        public ResultsForm(Tournament main)
        {
            InitializeComponent();
            this._main = main;
            SelectedPlayerList = new BindingList<Player>();
        }

        private void addResult_Load(object sender, EventArgs e)
        {
            SelectedPlayerList.Add((Player)comboBoxPlayer1.SelectedItem);
            SelectedPlayerList.Add((Player)comboBoxPlayer2.SelectedItem);

            comboBoxPlayer1.BindingContext = new BindingContext();
            comboBoxPlayer1.DataSource = _main.playerList;
            comboBoxPlayer1.DisplayMember = "Name";

            comboBoxPlayer2.BindingContext = new BindingContext();
            comboBoxPlayer2.DataSource = _main.playerList;
            comboBoxPlayer2.DisplayMember = "Name";

            comboBoxWinner.BindingContext = new BindingContext();
            comboBoxWinner.DataSource = SelectedPlayerList;
            comboBoxWinner.DisplayMember = "Name";

            //Blank winner field
            comboBoxWinner.Text = "";
        }

        private void comboBoxPlayer1_SelectedIndexChanged(object sender, EventArgs e)
        {   
            SelectedPlayerList.Clear();
            SelectedPlayerList.Add((Player)comboBoxPlayer1.SelectedItem);
            SelectedPlayerList.Add((Player)comboBoxPlayer2.SelectedItem);
            
            //Blank winner field
            comboBoxWinner.Text = "";
        }

        private void comboBoxPlayer2_SelectedIndexChanged(object sender, EventArgs e)
        {            
            SelectedPlayerList.Clear();
            SelectedPlayerList.Add((Player)comboBoxPlayer1.SelectedItem);
            SelectedPlayerList.Add((Player)comboBoxPlayer2.SelectedItem);

            //Blank winner field
            comboBoxWinner.Text = "";
        }

        private void buttonSubmit_Click(object sender, EventArgs e)
        {
            Player winningPlayer = SelectedPlayerList.Single(Player => Player.Name == comboBoxWinner.SelectedItem.ToString());
            Player losingPlayer = SelectedPlayerList.Single(Player => Player.Name != comboBoxWinner.SelectedItem.ToString());

            winningPlayer.Wins++;
            losingPlayer.Losses++;
            
            this.Close();
        }
        
    }
}
